﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Critr
{
    public partial class DatabaseQueryForm : Form
    {
        private List<ctlToggleableTextbox> ToggleableTextBoxes;

        public DatabaseQueryForm()
        {
            InitializeComponent();
            ToggleableTextBoxes = new List<ctlToggleableTextbox>() {
                ArtistTTextBox,
                TitleTTextbox,
                AlbumTTextBox,
                LogTTextBox,
                ClassTTextBox,
                ProgrammerTTextBox,
                ShowNameTTextBox
            };
        }

        private void LogTTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            int res;

            if (!int.TryParse(LogTTextBox.Text, out res))
            {
                errorProvider1.SetError(LogTTextBox, "Log # is not numeric.");
            }
        }

        private void QueryButton_Click(object sender, EventArgs e)
        {
            logdbQueryBuilder query = new logdbQueryBuilder();

            /* Input toggleable textboxes */
            foreach (ctlToggleableTextbox ctl in ToggleableTextBoxes)
            {
                if (ctl.Checked)
                {
                    query.Condition(ctl.Column, ctl.Text);
                }
            }

            /* Input checkboxes */
            if (CanconCheckBox.CheckState == CheckState.Checked) 
            {
                query.Condition("LogEntry.Cancon", "true");
            }
            else if (CanconCheckBox.CheckState == CheckState.Unchecked)
            {
                query.Condition("LogEntry.Cancon", "false");
            }

            if (HitCheckBox.CheckState == CheckState.Checked)
            {
                query.Condition("LogEntry.Hit", "true");
            }
            else if (HitCheckBox.CheckState == CheckState.Unchecked)
            {
                query.Condition("LogEntry.Hit", "false");
            }

            if (ChartCheckBox.CheckState == CheckState.Checked)
            {
                query.Condition("LogEntry.Chart", "true");
            }
            else if (ChartCheckBox.CheckState == CheckState.Unchecked)
            {
                query.Condition("LogEntry.Chart", "false");
            }

            MessageBox.Show(query.ToString());

            // TODO: Query database
        }

        private void LogTTextBox_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(LogTTextBox, string.Empty);
        }
    }
}
